package com.czr.frame.test;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import com.czr.frame.beans.Bank;
import com.czr.frame.beans.BankCard;
import com.czr.frame.beans.BankFq;
import com.czr.frame.beans.Movie;
import com.czr.frame.beans.User;
/**
 * 增删改查测试 spring hibernateTemplate
 * @author chenzhirong
 *
 */
import com.czr.frame.util.dao.GenericDAO;
public class DBMethom{
	
	private Logger log = Logger.getLogger(DBMethom.class);
	
	public static void main(String[] args) {
		
	}
	private GenericDAO dao;

	public void setDao(GenericDAO dao) {
		this.dao = dao;
	}
	
	private int count;

	/**
	 * 增加用户表数据
	 */
	public void addUser(){
		User user = new User();
		user.setUsername("rongzai");
		user.setUsercode("rongzai");
		user.setZt(1L);
		user.setCreatetime(new Date());
		user.setUpdatetime(new Date());
		user.setEmail("email");
		user.setJb(1L);
		user.setPassword("111111");
		dao.save(user);
	}
	
	/**
	 * 增加银行卡信息
	 */
	public void addBankCard(){
		BankCard bankCard = new BankCard();
		bankCard.setSsyh(1000000000000022L);
		bankCard.setZh(123456L);
		bankCard.setYhkzl(1L);
		bankCard.setSffq(2L);
		bankCard.setYe(100L);
		bankCard.setQk(10L);
		bankCard.setFqObjId(1L);
		bankCard.setCkrObjId(1000000000000020L);//持卡人
		bankCard.setCreatetime(new Date());
		bankCard.setUpdatetime(new Date());
		bankCard.setCjrObjId(1000000000000020L);//创建人
		dao.save(bankCard);
	}
	
	/**
	 * 增加所属银行
	 */
	public void addBank(){
		List<Bank> banks = new ArrayList<Bank>();
		Bank bank1 = new Bank();
		bank1.setBankcode("gfyh");
		bank1.setBankname("广发银行");
		Bank bank2 = new Bank();
		bank2.setBankcode("pfyh");
		bank2.setBankname("浦发银行");
		banks.add(bank1);
		banks.add(bank2);
		for (Bank bank : banks) {
			dao.save(bank);
		}
	}
	
	public void addBankFq(){
		BankFq bankFq = new BankFq();
		bankFq.setFqsj(new Date());
		bankFq.setFqzys(12L);//分期月数
		bankFq.setFqzje(1000L);//分期总金额
		bankFq.setFqly(49L);
		bankFq.setSfhkjs(1L);
		bankFq.setSyys(1L);
		bankFq.setSyje(100L);
		bankFq.setSxf(10L);
		bankFq.setBankObjId(1000000000000028L);
		dao.save(bankFq);
	}
	
	public void delete(Long id){
		User user = new User();
		user.setObjId(id);
		dao.delete(user);
		
	}
	
	/**
	 * 自动扫描文件夹下的文件 用途：自动扫描脚本输入路径，输出要执行的sql导入数据库；
	 */
	public String GeneratorMovie(String path) {
		File rootDir = new File(path);
		if (!rootDir.isDirectory()) {
			String url = rootDir.getAbsolutePath();
			
			log.info(String.format("路径：%s",url));
			String[] urls = url.split("\\\\");
			String tag = urls[urls.length-2];
			log.info(String.format("文件名：%s",urls[urls.length-1]));
			String zyPath = url.replace("G:\\jonee\\apache-tomcat-9.0.0.M20-windows-x64\\apache-tomcat-9.0.0.M20\\webapps\\SSHFrame", "");
			log.info(String.format("路径：zyPath %s",zyPath));
			String fileName = urls[urls.length-1];
			String[] sufixs = fileName.split("\\.");
			log.info(String.format("sufix：%s",sufixs[1]));
			
			Movie movie = new Movie();
			movie.setName(tag+"_"+fileName);
			movie.setSuffix(sufixs[1]);
			if("mp4".equals(sufixs[1]) || "MP4".equals(sufixs[1])){
				
				movie.setType("1");
				zyPath=zyPath.replace("\\", "/");
				movie.setUrl(zyPath);
				movie.setZt(1L);
				dao.save(movie);
			}
			
//			count++;
//			String newUrl=url.replace(fileName, count+".mp4");
//			new File(url);
//			File oldFile = new File(url);
//			if(!new File(newUrl).exists()){
//				oldFile.renameTo(new File(newUrl));
//			}
			
			
		} else {
			String[] fileList = rootDir.list();
			for (int i = 0; i < fileList.length; i++) {
				path = rootDir.getAbsolutePath() + "\\" + fileList[i];
				GeneratorMovie(path);
			}
		}
		return null;
	}
	
	/** *//**文件重命名 
	    * @param path 文件目录 
	    * @param oldname  原来的文件名 
	    * @param newname 新文件名 
	    */ 
	    public void renameFile(String path,String oldname,String newname){ 
	        if(!oldname.equals(newname)){//新的文件名和以前文件名不同时,才有必要进行重命名 
	            File oldfile=new File(path+"/"+oldname); 
	            File newfile=new File(path+"/"+newname); 
	            if(!oldfile.exists()){
	                return;//重命名文件不存在
	            }
	            if(newfile.exists())//若在该目录下已经有一个文件和新文件名相同，则不允许重命名 
	                System.out.println(newname+"已经存在！"); 
	            else{ 
	                oldfile.renameTo(newfile); 
	            } 
	        }else{
	            System.out.println("新文件名和旧文件名相同...");
	        }
	    }
}
